package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.pojo.Employee;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/page")
    public R<IPage<Employee>> getPage(@RequestParam("page") Integer page,
                                      @RequestParam("pageSize") Integer pageSize){
        System.out.println(page+"----------"+pageSize);
        IPage page1=new Page(page,pageSize);
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
//        lqw.like((name!=null &&name!=""),Employee::getName,name);
        IPage page2 =categoryService.page(page1);
        System.out.println(page2.getCurrent());
        System.out.println(page2.getSize());
        System.out.println(page2.getPages());
        System.out.println(page2.getTotal());
        System.out.println(page2.getRecords());
        return R.success(page2);

    }
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category数据：{}",category);
        boolean save = categoryService.save(category);
        if (save){
            return  R.success("新增分类成功");
        }else{
            return R.error("新增分类失败");
        }

    }
    @DeleteMapping
    public R<String> deleteByIds(Long ids){
        System.out.println("ids是..........."+ids);
        Integer b = categoryService.deleteService(ids);
        log.info("返回的b为：{}",b);
        if (b>0){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }
    @PutMapping
    public R<String> updateId(@RequestBody Category category){
        log.info("要修改的category的id为：{}",category);
        boolean b = categoryService.updateById(category);
        if(b){
            return R.success("修改分类成功");
        }else {
            return R.error("修改分类失败");
        }

    }
    @GetMapping("/list")
    public R<List<Category>> listR(Category category){
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.eq(category.getType()!=null,Category::getType,category.getType());
        lqw.orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(lqw);
        return R.success(list);
    }

}
